﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HZ.BidSystem.Model;
using HZ.Data;

namespace HZ.BidSystem.DAL
{
    /// <summary>
    /// 报价统计处理
    /// </summary>
    public class DAL_BidStatus
    {
        public static int RefreshStatistic(int companyID)
        {
            var bidstatus = HZ.Data.DAL_Helper.GetModelByID<STAT_BidStatus>(new STAT_BidStatus() { CompanyId = companyID });

            var dbhlepr = HZ.Data.DbHelperFactory.Create();
            int allcount = (int)dbhlepr.ExecuteScalar(string.Format("SELECT COUNT(BidId) FROM TB_Project_Bid WHERE CompanyId = {0}", companyID));

            int bidcount = (int)dbhlepr.ExecuteScalar(string.Format("SELECT COUNT(BidId) FROM TB_Project_Bid WHERE CompanyId = {0} AND EncryptPrice IS NOT NULL", companyID));
            if (bidstatus == null)
            {
                bidstatus = new STAT_BidStatus() { CompanyId = companyID };
                bidstatus.AllCount = allcount;
                bidstatus.BidCount = bidcount;
                var company = HZ.Data.DAL_Helper.GetModelByID<Company>(new Company() { CompanyId = companyID });
                bidstatus.CompanyName = company.CompanyName;
                bidstatus.UserCode = company.UserCode;
                return HZ.Data.DAL_Helper.OperateTableSQL(bidstatus, HZ.Data.TableOperator.Insert);
            }
            else
            {
                bidstatus.AllCount = allcount;
                bidstatus.BidCount = bidcount;
                return HZ.Data.DAL_Helper.OperateTableSQL(bidstatus, HZ.Data.TableOperator.Update);
            }
        }

        public static int GetConfirmedCompanyCount()
        {
            return (int)DbHelperFactory.Create().ExecuteScalar("SELECT COUNT(*) FROM dbo.STAT_BidStatus WHERE ConfirmStatus = 1");
        }

        public static int GetBidCompanyCount()
        {
            return (int)DbHelperFactory.Create().ExecuteScalar("SELECT COUNT(*) FROM dbo.STAT_BidStatus  WHERE BidCount > 0 AND ConfirmStatus = 1 ");
        }

        public static int GetAllCompnayCount()
        {
            return (int)DbHelperFactory.Create().ExecuteScalar("SELECT COUNT(*) FROM dbo.STAT_BidStatus");
        }

        public static System.Data.DataTable GetBidCountByUserCode(string usercode)
        {
            return DbHelperFactory.Create().ExecuteTable(string.Format("SELECT allcount,bidcount FROM dbo.STAT_BidStatus where usercode='{0}'", usercode));
        }

        /// <summary>
        /// 获取报价情况
        /// </summary>
        /// <param name="where"></param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public static System.Data.DataTable GetBidStatusNew(string where, int currentpage, int pagesize, out int totalrecord)
        {
            string strsql = string.Format(@"select CompanyCode_Tb,CompanyName_Tb,COUNT(BidId) AllCount,Sum(case when EncryptPrice Is not null then 1 else 0 end) BidCount  from View_DecStatus {0} group by CompanyCode_Tb,CompanyName_Tb order by CompanyCode_Tb,CompanyName_Tb", where);
            HZ.Data.PageDescribe des = new HZ.Data.PageDescribe(strsql, pagesize);
            des.CurrentPage = currentpage;
            totalrecord = des.RecordCount;
            return des.GetSpecailPage();
        }

        /// <summary>
        /// 获取报价汇总统计
        /// </summary>
        /// <returns></returns>
        public static System.Data.DataTable GetBidCount()
        {
            return DbHelperFactory.Create().ExecuteTable(@" select COUNT(distinct CompanyId_Tb) 企业总数,
                            count(distinct (case when EncryptPrice Is not null then CompanyId_Tb else null end )) 已报价企业数,
                             count(distinct (case when ConfirmStatus=1 and EncryptPrice Is not null then CompanyId_Tb else null end )) 已报价且确认企业数,
                             COUNT(BidId) 产品总数,SUM(case when EncryptPrice Is not null then 1 else 0 end) 已报价产品数,
                            SUM(case when ConfirmStatus=1 and EncryptPrice Is not null then 1 else 0 end) 已报价且确认产品数
                             from View_DecStatus ");
        }

        /// <summary>
        /// 获取解密汇总统计
        /// </summary>
        /// <returns></returns>
        public static System.Data.DataTable GetDecCount()
        {
            return DbHelperFactory.Create().ExecuteTable(@" select COUNT(distinct CompanyId_Tb) 企业总数,
                              count(distinct (case when DecStatus=1 and DecPrice is not null then CompanyId_Tb else null end )) 已解密企业数,
                             COUNT(BidId) 产品总数,SUM(case when DecStatus=1 and DecPrice Is not null then 1 else 0 end) 产品解密数
                             from View_DecStatus where ConfirmStatus=1 ");
        }
    }
}
